﻿@using System.Data
@{
    ViewBag.Title = "";
    Layout = "~/Areas/Admin/Views/Shared/AdminLayout.cshtml";
    DataTable table = ViewData["menu"] as DataTable;
    
}

<input name="id" id="id" type="hidden" value="0" />
<table style="float: left;font-size: 12px;">
    <tr>
        <td><label>菜单名称</label></td>
        <td><input type="text" id="name" name="name" style="width: 200px;" /></td>
    </tr>
    <tr>
        <td><label>隶属菜单</label></td>
        <td>
            <select id="pmenu" style="width: 100px;">
                <option value="0">父级菜单</option>
                @{
                    foreach (DataRow row in table.Rows)
                    {
                        <option value="@row["Id"]">@row["Name"]</option>
                    }

                }
            </select>
        </td>
    </tr>
    <tr>
        <td><label>链接地址</label></td>
        <td><input type="text" id="url" name="url" style="width: 300px;" /></td>
    </tr>
    <tr>
        <td><label>序号</label></td>
        <td><input type="text" id="order" style="width: 100px;" /></td>
    </tr>
    <tr>
        <td><label>备注</label></td>
        <td><input type="text" id="remark" style="width: 300px;" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <div class="botton" style="margin-left: 50px;" onclick="edit()">保存</div>
            <div class="botton" style="margin-left: 50px;" onclick="javascript: history.go(-1);" >取消</div>
        </td>      
    </tr>
</table>

<script>
   var id = "@ViewData["id"]";
    if (parseFloat(id) > 0) {
        $.post("@Url.Action("GetOneData")", { id: id }, function(data) {
           var obj = jQuery.parseJSON(data);
           $("#id").val(obj[0].Id);
           $("#name").val(obj[0].Name);
           $("#pmenu").val(obj[0].Pid);
           $("#url").val(obj[0].Url);
           $("#order").val(obj[0].Order);
           $("#remark").val(obj[0].Remark);
       });
   }

   function edit() {
       var name = $("#name").val();
       var url = $("#url").val();
       var pid = $("#pmenu").val();
       var remark = $("#remark").val();
       var order = $("#order").val();

       if (name.replace(/\ +/g, "") == "") {
           alert("请输入菜单名称！");
           return false;
       }
       if (url.replace(/\ +/g, "") == "") {
           alert("请输入链接地址！");
           return false;
       }
       if (remark.length > 100) {
           alert("备注只能输入100个字！");
           return false;
       }
       var test = /^\+?[1-9][0-9]*$/;
       if (!test.test(order)) {
           alert("序号输入有误！");
           return false;
       }
       $.post("@Url.Action("Edit")", { id: id, url: url, name: name, pid: pid, remark: remark, order: order }, function (row) {
           if (row > 0) {
               alert("操作成功！");

           } else {
               alert("添加失败！");
           }
           location.href = "@Url.Action("Index")";

        });
    }

</script>

